import * as http from './http'

const handleResponse = res => {
  const { data } = res
  if (!data) {
    console.error(res.config.method, res.config.url, data)
    throw data || res
  }
  return data
}

class Api {
  constructor (baseUrl) {
    this.baseUrl = baseUrl
    this.$axios = http.default
  }
  _getUrl (url) {
    return `${this.baseUrl}${url}`
  }
  async get (url, params) {
    let res = await http.get(this._getUrl(url), params)
    return handleResponse(res)
  }
  async post (url, data) {
    let res = await http.post(this._getUrl(url), data)
    return handleResponse(res)
  }
  async postFile (url, data) {
    let res = await http.postFile(this._getUrl(url), data)
    return handleResponse(res)
  }
  async postForm (url, data) {
    let res = await http.postForm(this._getUrl(url), data)
    return handleResponse(res)
  }
}

export const api = new Api('/api')
